package com.jinlejia.erp.ui.mine.transfer;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityTransferAuditBinding;
import com.jinlejia.erp.ui.common.ProgressDialog;
import com.jinlejia.erp.utils.DataFilter;
import com.weight.CustomToast;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 异动审核
 */
public class TransferAuditActivity extends AppCompatActivity {


    String userbdCode;
    int auditState = 2;
    ProgressDialog progressDialog;
    private ActivityTransferAuditBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_transfer_audit);
        binding.setEvent(this);
        userbdCode = getIntent().getStringExtra("userbdCode");
        progressDialog = new ProgressDialog(this, "请稍后...");
        binding.reason.addTextChangedListener(new TextWatcher() {
            private CharSequence tmp;
            private int SelectionStart;
            private int SelectionEnd;

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                tmp = charSequence;
            }

            @Override
            public void afterTextChanged(Editable editable) {
                int number = 200 - editable.length();
                binding.surplusCount.setText(number + "/200");
                SelectionStart = binding.reason.getSelectionStart();
                SelectionEnd = binding.reason.getSelectionEnd();
                if (tmp.length() > 200) {
                    editable.delete(SelectionStart - 1, SelectionEnd);
                    int tempSelection = SelectionEnd;
                    binding.reason.setText(editable);
                    binding.reason.setSelection(tempSelection);//设置光标在最后
                }
            }
        });

        binding.radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.audit_pass:
                    auditState = 2;
                    break;
                case R.id.audit_no_pass:
                    auditState = 1;
                    break;
            }
        });

    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.submit:
                if (auditState == 1 && TextUtils.isEmpty(binding.reason.getText().toString())) {
                    CustomToast.showToast(TransferAuditActivity.this, "请填写不通过原因", 1500);
                } else {
                    postData();
                }
                break;
        }
    }

    private void postData() {
        progressDialog.show();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userbdCode", userbdCode);
        jsonObject.put("auditState", auditState);
        jsonObject.put("rejectReason", binding.reason.getText().toString());

        OkHttpUtils.postString()
                .url(ApiConfig.TRANSFER_AUDIT)
                .content(jsonObject.toString())
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        call.cancel();
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        if (DataFilter.filter(TransferAuditActivity.this, response)) {
                            CustomToast.showToast(TransferAuditActivity.this, "提交成功", 1500);
                            finish();
                        }
                    }
                });

    }
}
